<?php

namespace App\Shop\Models;

use Dcat\Admin\Admin;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;

/**
 * Class Administrator.
 *
 * @property $id
 * @property $user_id
 * @property $username
 * @property $password
 * @property $shop_id
 * @property $avatar
 * @property Role[] $roles
 * @property string|null $remember_token
 * @property int $status
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read int|null $roles_count
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator query()
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereAvatar($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereRememberToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereShopId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereUsername($value)
 * @mixin \Eloquent
 * @property int $cid 分类ID
 * @property string $name 门店名称
 * @property string $phone 门店电话
 * @property string $address 详细地址
 * @property string $longitude 经度
 * @property string $latitude 纬度
 * @property mixed|null $images 门店图片组
 * @property float $rate 洗车服务收入分成百分比
 * @property string $money 可提现余额
 * @property string $used_money 已提现余额
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereCid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereImages($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereLatitude($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereLongitude($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereUsedMoney($value)
 * @property string|null $fullname 负责人名称
 * @property int $province_id 省ID
 * @property int $city_id 市ID
 * @property int $district_id 区ID
 * @property int $distance 距离公里数
 * @property string $distance_price 超出限制收费(每公里)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereCityId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereDistance($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereDistancePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereDistrictId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereFullname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Administrator whereProvinceId($value)
 */
class Administrator extends Model implements AuthenticatableContract
{
    use Authenticatable,
        HasPermissions,
        HasDateTimeFormatter;

    const DEFAULT_ID = 1;

    protected $fillable = ['username', 'password', 'name', 'avatar','shop_id'];

    /**
     * Create a new Eloquent model instance.
     *
     * @param  array  $attributes
     */
    public function __construct(array $attributes = [])
    {
        $this->init();

        parent::__construct($attributes);
    }

    protected function init()
    {
        $connection = config('shop.database.connection') ?: config('database.default');

        $this->setConnection($connection);

        $this->setTable(config('shop.database.users_table'));
    }

    public static function booted()
    {
        self::creating(function ($row){
            if (auth('shop')->user()){
                $admin = Admin::user();
                $isShop = $admin->isAdministrator();
                if ($isShop){
                    $shop_id = $admin->id;
                }else{
                    $shop_id = $admin->shop_id;
                }
            }else{
                $shop_id = 0;
            }
            $row->shop_id = $shop_id;
        });
    }

    /**
     * Get avatar attribute.
     *
     * @return mixed|string
     */
    public function getAvatar()
    {
        $avatar = $this->avatar;

        if ($avatar) {
            if (! URL::isValidUrl($avatar)) {
                $avatar = Storage::disk(config('admin.upload.disk'))->url($avatar);
            }

            return $avatar;
        }

        return admin_asset(config('shop.default_avatar') ?: '@admin/images/default-avatar.jpg');
    }

    /**
     * A user has and belongs to many roles.
     *
     * @return BelongsToMany
     */
    public function roles(): BelongsToMany
    {
        $pivotTable = config('shop.database.role_users_table');

        $relatedModel = config('shop.database.roles_model');

        return $this->belongsToMany($relatedModel, $pivotTable, 'user_id', 'role_id')->withTimestamps();
    }

    /**
     * 判断是否允许查看菜单.
     *
     * @param  array|Menu  $menu
     * @return bool
     */
    public function canSeeMenu($menu)
    {
        return true;
    }

//    public function shop(){
//        return $this->hasOne(Shop::class,"id","shop_id");
//    }
}
